package com.jsmy.softapp.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.softapp.R;
import com.jsmy.softapp.activity.CustomerActivity;
import com.jsmy.softapp.adapter.DailyFragmentAdapter;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.YWgtjhBean;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.DailyPresenter;
import com.jsmy.softapp.util.DialogUtils;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.DailyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/23.
 * Time: 16:22.
 * To change for file | settings | file and code templates.
 */

@Route(path = AR.F_KHDAILY)
public class KHDailyFragment extends BaseFragment<DailyView, DailyPresenter> implements DailyView {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    DailyFragmentAdapter adapter;
    List<YWgtjhBean.DataBean.ListBean> list;
    int pageindex = 1;

    @Override
    protected DailyPresenter createPresenter() {
        return new DailyPresenter();
    }

    @Override
    protected int setView() {
        return R.layout.fragment_daily;
    }

    @Override
    protected void init(View view) {
        adapter = new DailyFragmentAdapter();
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                presenter.getkhzlgtrzlist(((CustomerActivity)mActivity).kid,pageindex);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                presenter.getkhzlgtrzlist(((CustomerActivity)mActivity).kid,pageindex);

            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                presenter.savedelgtrzinfo(list.get(position).getRid());
                DialogUtils.showDeleteDialog(mActivity,presenter,((YWgtjhBean.DataBean.ListBean)adapter.getData().get(position)).getRid());
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        presenter.getkhzlgtrzlist(((CustomerActivity)mActivity).kid,pageindex);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type){
            case API.APP_KHZL_GTRZ_LIST:
                list = ((YWgtjhBean)object).getData().getList();
                if (pageindex == 1){
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                }else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
            case API.APP_SAVE_DEL_GTRZ_INFO:
                ToastUtil.showShort(mActivity,"删除成功！");
                updata();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        presenter.model.disposable.clear();
        super.onDestroyView();
    }

    public void updata(){
        pageindex = 1;
        presenter.getkhzlgtrzlist(((CustomerActivity)mActivity).kid,pageindex);
    }
}
